package com.example.it.studyimagerecycle;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, Runnable {



    private RadioButton rb1, rb2, rb3;
    private ViewPager imageViewPager;


    private List<RadioButton> radiobuttionList;// 图片轮播的点的集合

    private List<Integer> drawIdList; //图片id的集合

    private List<ImageView> imageViewList; //要在ViewPager上显示的内容

    private int pageIndex = 0; //viewpagr页面的索引

    private int countDown = 3; //切换页面的倒计时

    /*
    * 获取我们需要的view
    * */
    private void initFindView() {
        rb1 = (RadioButton) this.findViewById(R.id.dot1);
        rb2 = (RadioButton) this.findViewById(R.id.dot2);
        rb3 = (RadioButton) this.findViewById(R.id.dot3);

        imageViewPager = (ViewPager) this.findViewById(R.id.img_cycle_viewpager);
    }

    /*
    * 将屏幕上的点整合起来
    * */
    private void initDots() {
        radiobuttionList = new ArrayList<RadioButton>();
        radiobuttionList.add(rb1);
        radiobuttionList.add(rb2);
        radiobuttionList.add(rb3);

        for (RadioButton rb : radiobuttionList) {
            rb.setClickable(false);
        }
    }

    /*
    * 初始化要加载的图片
    * */
    private void initImage() {
        this.drawIdList = new ArrayList<Integer>();
        drawIdList.add(R.drawable.grape);
        drawIdList.add(R.drawable.strawberry);
        drawIdList.add(R.drawable.watermelon);
    }

    /*
    * 初始化ViewPage所需要的ImageView
    * */
    private void initImageView() {
        imageViewList = new ArrayList<ImageView>();
        for (int i = 0; i < 3; i++) {
            //初始化ImageView的属性
            ImageView imageView = new ImageView(this);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(-1, -1);
            imageView.setLayoutParams(lp);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            this.imageViewList.add(imageView);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFindView();
        initDots();
        initImage();
        initImageView();

        this.imageViewPager.addOnPageChangeListener(this);
        this.imageViewPager.setAdapter(new MyPagerAdapter(this, imageViewList, drawIdList));

        new Thread(this).start();
    }

    //region ViewPager的页面切换的事件
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /*
    * 页面被选中的时候触发
    * 我们要在这个页面中处理，当我们手动滑动页面的时候要处理的业务逻辑
    * */
    @Override
    public void onPageSelected(int position) {

        RadioButton radioButton = radiobuttionList.get(position);
        radioButton.setChecked(true);
        //重置倒计时
        this.countDown = 3;
        //重新设置pageIndex
        this.pageIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    //endregion

    /*
    * 倒计时进行图片的切换
    * */
    @Override
    public void run() {
        while (MainActivity.this.isDestroyed() == false) {
            SystemClock.sleep(1000l);
            countDown--;
            if (countDown == 0) {
                this.pageIndex++;
                if (pageIndex == this.radiobuttionList.size())
                    this.pageIndex = 0;

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //设置选中项
                        imageViewPager.setCurrentItem(pageIndex);
                    }

                });
            }
        }
    }
}
